﻿//using System;
//using System.Collections.Generic;
//using FireStarter.Base;
//using FireStarterModeller.BO;
//using FireStarterModeller.Logic;
//using FireStarterModeller.Test.Logic;
//using FireStarterModeller.UI;
//using FireStarterModeller.UI.ViewEditor;
//using Habanero.Base;
//using Habanero.BO;
//using Habanero.BO.ClassDefinition;
//using Habanero.Faces.Base;
//using Habanero.Faces.Win;
//using Habanero.Util;
//using NUnit.Framework;
//using Rhino.Mocks;
//using Rhino.Mocks.Interfaces;

//namespace FireStarterModeller.Test.UI.ViewEditor
//{

//    [TestFixture]
//    public class TestUIViewsEditorControl
//    {
//        [TestFixtureSetUp]
//        public void SetupFixture()
//        {
//            UITestUtils.SetupFixture();
//        }

//        [SetUp]
//        public virtual void SetupTest()
//        {
//            UITestUtils.SetupTest();
//        }

//        private static IControlFactory GetControlFactory()
//        {
//            return GlobalUIRegistry.ControlFactory;
//        }

//        private static UIViewsEditorControl CreateUIViewsEditorControl()
//        {
//            IUIXmlEditorControl xmlEditorControl = MockRepository.GenerateStub<IUIXmlEditorControl>();
//            IUIPreviewControl previewControl = MockRepository.GenerateStub<IUIPreviewControl>();
//            IUIViewSelectorControl selectorControl = MockRepository.GenerateStub<IUIViewSelectorControl>();
//            IConverter<DMSolution, ClassDefCol> converter = MockRepository.GenerateStub<IConverter<DMSolution, ClassDefCol>>();

//            return new UIViewsEditorControl(GetControlFactory(), xmlEditorControl, previewControl, selectorControl, converter);
//        }

//        [Test]
//        public void Test_Construct()
//        {
//            //---------------Set up test pack-------------------
//            IControlFactory controlFactory = GetControlFactory();
//            IUIXmlEditorControl xmlEditorControl = MockRepository.GenerateStub<IUIXmlEditorControl>();
//            IUIPreviewControl previewControl = MockRepository.GenerateStub<IUIPreviewControl>();
//            IUIViewSelectorControl selectorControl = MockRepository.GenerateStub<IUIViewSelectorControl>();
//            IConverter<DMClass, IClassDef> converter = MockRepository.GenerateStub<IConverter<DMClass, IClassDef>>();
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            UIViewsEditorControl viewsEditorControl = new UIViewsEditorControl(controlFactory, xmlEditorControl, previewControl, selectorControl, converter);
//            //---------------Test Result -----------------------
//            Assert.AreSame(controlFactory, viewsEditorControl.ControlFactory);
//            Assert.AreSame(xmlEditorControl, viewsEditorControl.XMLEditorControl);
//            Assert.AreSame(previewControl, viewsEditorControl.PreviewControl);
//            Assert.AreSame(selectorControl, viewsEditorControl.SelectorControl);
//            Assert.AreSame(converter, viewsEditorControl.DMClassToClassDefConverter);
//            Assert.IsNull(viewsEditorControl.ClassDef);
//        }

//        [Test]
//        public void Test_Layout()
//        {
//            IControlFactory controlFactory = GetControlFactory();
//            IUIXmlEditorControl xmlEditorControl = MockRepository.GenerateStub<IUIXmlEditorControl>();
//            IUIPreviewControl previewControl = MockRepository.GenerateStub<IUIPreviewControl>();
//            IUIViewSelectorControl selectorControl = MockRepository.GenerateStub<IUIViewSelectorControl>();
//            IConverter<DMClass, IClassDef> converter = MockRepository.GenerateStub<IConverter<DMClass, IClassDef>>();
//            selectorControl.Enabled = true;
//            xmlEditorControl.Enabled = true;
//            previewControl.Enabled = true;
//            //---------------Assert Precondition----------------
//            Assert.IsTrue(selectorControl.Enabled);
//            Assert.IsTrue(xmlEditorControl.Enabled);
//            Assert.IsTrue(previewControl.Enabled);
//            //---------------Execute Test ----------------------
//            UIViewsEditorControl viewsEditorControl = new UIViewsEditorControl(controlFactory, xmlEditorControl, previewControl, selectorControl, converter);
//            //---------------Test Result -----------------------
//            Assert.IsFalse(selectorControl.Enabled);
//            Assert.IsFalse(xmlEditorControl.Enabled);
//            Assert.IsFalse(previewControl.Enabled);
//        }

//        [Test]
//        public void Test_EditUIViews_ShouldShouldSetupSelectorControlViews()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
//            UIFormCreator creator = new UIFormCreator();
//            UIFormInfo formInfo = creator.CreateUIForm(dmClass);
//            UIView view = dmClass.UIViews.CreateBusinessObject();
//            view.UIFormInfo = formInfo;

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIViewSelectorControl selectorControl = viewsEditorControl.SelectorControl;
//            BusinessObjectCollection<UIView> uiViews = dmClass.UIViews;
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(uiViews);
//            //---------------Test Result -----------------------
//            Assert.AreEqual(uiViews, selectorControl.UIViews);
//        }

//        [Test]
//        public void Test_EditUIViews_ShouldResetClassDef()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();
//            UIFormCreator creator = new UIFormCreator();
//            UIFormInfo formInfo = creator.CreateUIForm(dmClass);
//            UIView view = dmClass.UIViews.CreateBusinessObject();
//            view.UIFormInfo = formInfo;

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IClassDef classDef = MockRepository.GenerateStub<IClassDef>();
//            viewsEditorControl.ClassDef = classDef;
//            //---------------Assert PreConditions---------------
//            Assert.IsNotNull(viewsEditorControl.ClassDef);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Test Result -----------------------
//            // The Converter should not get called because the stub Selector does not throw the SelectionChanged event
//            viewsEditorControl.DMClassToClassDefConverter.AssertWasNotCalled(t => t.Convert(dmClass.Solution));
//            Assert.IsNull(viewsEditorControl.ClassDef);
//        }

//        [Test]
//        public void Test_EditUIViews_WhenUIViewsListIsNotNull_ShouldEnableSelectorControl()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            //---------------Assert PreConditions---------------
//            Assert.IsFalse(viewsEditorControl.SelectorControl.Enabled);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Test Result -----------------------
//            Assert.IsTrue(viewsEditorControl.SelectorControl.Enabled);
//        }

//        [Test]
//        public void Test_EditUIViews_WhenUIViewsListIsNotNull_ShouldSelectFirstItemInSelectorControl()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            //---------------Assert PreConditions---------------
//            Assert.IsNull(viewsEditorControl.SelectorControl.SelectedUIView);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Test Result -----------------------
//            Assert.AreSame(dmClass.UIViews[0], viewsEditorControl.SelectorControl.SelectedUIView);
//        }

//        [Test]
//        public void Test_EditUIViews_WhenUIViewsListIsEmpty_ShouldSelectNullInSelectorControl()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClassWithTwoViews = TestUtilsLogic.CreateClassWithTwoUIViews();
//            DMClass dmClass = TestUtilsLogic.CreateClassWithNoUIViews();
            
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            viewsEditorControl.EditUIViews(dmClassWithTwoViews.UIViews);
//            //---------------Assert PreConditions---------------
//            Assert.AreEqual(0, dmClass.UIViews.Count);
//            Assert.IsNotNull(viewsEditorControl.SelectorControl.SelectedUIView);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Test Result -----------------------
//            Assert.IsNull(viewsEditorControl.SelectorControl.SelectedUIView);
//        }

//        [Test]
//        public void Test_EditUIViews_WhenUIViewsListIsEmpty_ShouldEnableSelectorControl()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            dmClass.UIViews.Clear();
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            //---------------Assert PreConditions---------------
//            Assert.AreEqual(0, dmClass.UIViews.Count);
//            Assert.IsFalse(viewsEditorControl.SelectorControl.Enabled);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Test Result -----------------------
//            Assert.IsTrue(viewsEditorControl.SelectorControl.Enabled);
//        }

//        [Test]
//        public void Test_EditUIViews_WhenUIViewsListIsNull_ShouldDisableSelectorControl()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Assert PreConditions---------------
//            Assert.IsTrue(viewsEditorControl.SelectorControl.Enabled);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(null);
//            //---------------Test Result -----------------------
//            Assert.IsFalse(viewsEditorControl.SelectorControl.Enabled);
//        }

//        [Test]
//        public void Test_EditUIViews_WhenUIViewsListIsNull_ShouldSelectNullInSelectorControl()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Assert PreConditions---------------
//            Assert.IsNotNull(viewsEditorControl.SelectorControl.SelectedUIView);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.EditUIViews(null);
//            //---------------Test Result -----------------------
//            Assert.IsNull(viewsEditorControl.SelectorControl.SelectedUIView);
//        }

//        [Test]
//        public void Test_UIViewSelected_WhenConvertReturnsClassDef_ShouldEnablePreview()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            UIView view2 = dmClass.UIViews[1];

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IEventRaiser uiViewSelected_EventRaiser = viewsEditorControl.SelectorControl.GetEventRaiser(control => control.UIViewSelected += null);
//            IClassDef classDef = MockRepository.GenerateStub<IClassDef>();
//            ClassDefCol classes = new ClassDefCol();
//            classes.Add(classDef);
//            viewsEditorControl.DMClassToClassDefConverter.Stub(t => t.Convert(dmClass.Solution)).Return(classes);

//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Assert PreConditions---------------
//            Assert.IsFalse(viewsEditorControl.PreviewControl.Enabled);
//            Assert.IsFalse(viewsEditorControl.PreviewFormButton.Enabled);
//            Assert.IsFalse(viewsEditorControl.PreviewGridButton.Enabled);
//            //---------------Execute Test ----------------------
//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(view2));
//            //---------------Test Result -----------------------
//            Assert.IsTrue(viewsEditorControl.PreviewControl.Enabled);
//            Assert.IsTrue(viewsEditorControl.PreviewFormButton.Enabled);
//            Assert.IsTrue(viewsEditorControl.PreviewGridButton.Enabled);
//        }

//        [Test]
//        public void Test_UIViewSelected_WhenConvertReturnsNullClassDef_ShouldDisablePreview()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            UIView view1 = dmClass.UIViews[0];
//            UIView view2 = dmClass.UIViews[1];

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IEventRaiser uiViewSelected_EventRaiser = viewsEditorControl.SelectorControl.GetEventRaiser(control => control.UIViewSelected += null);
//            IClassDef classDef = MockRepository.GenerateStub<IClassDef>();
//            viewsEditorControl.DMClassToClassDefConverter.Stub(t => t.Convert(dmClass)).Return(classDef);
//            viewsEditorControl.EditUIViews(dmClass.UIViews);

//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(view1));
//            viewsEditorControl.DMClassToClassDefConverter.Stub(t => t.Convert(dmClass)).Return(null).Repeat.Any();
//            //---------------Assert PreConditions---------------
//            Assert.IsTrue(viewsEditorControl.PreviewControl.Enabled);
//            Assert.IsTrue(viewsEditorControl.PreviewFormButton.Enabled);
//            Assert.IsTrue(viewsEditorControl.PreviewGridButton.Enabled);
//            //---------------Execute Test ----------------------
//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(view2));
//            //---------------Test Result -----------------------
//            Assert.IsFalse(viewsEditorControl.PreviewControl.Enabled);
//            Assert.IsFalse(viewsEditorControl.PreviewFormButton.Enabled);
//            Assert.IsFalse(viewsEditorControl.PreviewGridButton.Enabled);
//        }

//        [Test]
//        public void Test_UIViewSelected_ShouldSetUIViewOnEditor()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            UIView view2 = dmClass.UIViews[1];

//            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();

//            IUIXmlEditorControl xmlEditorControl = MockRepository.GenerateStub<IUIXmlEditorControl>();
//            IUIPreviewControl previewControl = MockRepository.GenerateStub<IUIPreviewControl>();
//            IUIViewSelectorControl selectorControl = MockRepository.GenerateStub<IUIViewSelectorControl>();
//            IEventRaiser uiViewSelected_EventRaiser = selectorControl.GetEventRaiser(control => control.UIViewSelected += null);
//            UIViewsEditorControl viewsEditorControl = new UIViewsEditorControl(GetControlFactory(), xmlEditorControl, previewControl, selectorControl, converter);
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Assert PreConditions---------------     
//            Assert.IsNull(xmlEditorControl.CurrentUIView);
//            //---------------Execute Test ----------------------
//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(view2));
//            //---------------Test Result -----------------------
//            Assert.AreSame(view2, xmlEditorControl.CurrentUIView);
//        }

//        [Test]
//        public void Test_UIViewSelected_WhenItemSelected_ShouldEnableEditor()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            UIView view2 = dmClass.UIViews[1];

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIViewSelectorControl selectorControl = viewsEditorControl.SelectorControl;
//            IEventRaiser uiViewSelected_EventRaiser = selectorControl.GetEventRaiser(control => control.UIViewSelected += null);
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Assert PreConditions---------------
//            Assert.IsFalse(viewsEditorControl.XMLEditorControl.Enabled);
//            //---------------Execute Test ----------------------
//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(view2));
//            //---------------Test Result -----------------------
//            Assert.IsTrue(viewsEditorControl.XMLEditorControl.Enabled);
//        }

//        [Test]
//        public void Test_UIViewSelected_WhenItemSelectedIsNull_ShouldDisableEditor()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithTwoUIViews();
//            UIView view2 = dmClass.UIViews[1];

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIViewSelectorControl selectorControl = viewsEditorControl.SelectorControl;
//            IEventRaiser uiViewSelected_EventRaiser = selectorControl.GetEventRaiser(control => control.UIViewSelected += null);
//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(view2));
//            //---------------Assert PreConditions---------------
//            Assert.IsTrue(viewsEditorControl.XMLEditorControl.Enabled);
//            //---------------Execute Test ----------------------
//            uiViewSelected_EventRaiser.Raise(viewsEditorControl, new BOEventArgs<UIView>(null));
//            //---------------Test Result -----------------------
//            Assert.IsFalse(viewsEditorControl.XMLEditorControl.Enabled);
//        }

//        [Test]
//        [Ignore("This does not work because the perform click for the button is not working")]
//        public void Test_PreviewGridButton_WhenHasClassDef_ShouldRunPreviewGrid()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            string viewName = dmClass.UIViews[0].ViewName;

//            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();
//            ClassDef classDef = (ClassDef)converter.Convert(dmClass);

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIXmlEditorControl xmlEditorControl = viewsEditorControl.XMLEditorControl;
//            IUIPreviewControl previewControl = viewsEditorControl.PreviewControl;

//            IUIDef uiDef = classDef.UIDefCol[viewName];
//            IUIGrid uiGrid = uiDef.UIGrid;
//            xmlEditorControl.Stub(control => control.CurrentUIDef).Return(uiDef);

//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            viewsEditorControl.ClassDef = classDef;
//            //---------------Execute Test ----------------------
//            viewsEditorControl.PreviewGridButton.PerformClick();
//            //---------------Test Result -----------------------
//            previewControl.AssertWasCalled(control => control.PreviewGrid(classDef, uiGrid));
//        }

//        [Test]
//        public void Test_PreviewGridButton_WhenNotHasClassDef_ShouldNotRunPreviewGrid()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            string viewName = dmClass.UIViews[0].ViewName;

//            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();
//            ClassDef classDef = (ClassDef)converter.Convert(dmClass);
            
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIXmlEditorControl xmlEditorControl = viewsEditorControl.XMLEditorControl;
//            IUIPreviewControl previewControl = viewsEditorControl.PreviewControl;

//            IUIDef uiDef = classDef.UIDefCol[viewName];
//            xmlEditorControl.Stub(control => control.CurrentUIDef).Return(uiDef);

//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            viewsEditorControl.ClassDef = null;
//            //---------------Execute Test ----------------------
//            viewsEditorControl.PreviewGridButton.PerformClick();
//            //---------------Test Result -----------------------
//            previewControl.AssertWasNotCalled(control => control.PreviewGrid(Arg<IClassDef>.Is.Anything, Arg<IUIGrid>.Is.Anything));
//        }

//        [Test]
//        [Ignore("This does not work because the perform click for the button is not working")]
//        public void Test_PreviewFormButton_WhenHasClassDef_ShouldRunPreviewGrid()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            string viewName = dmClass.UIViews[0].ViewName;

//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIXmlEditorControl xmlEditorControl = viewsEditorControl.XMLEditorControl;
//            IUIPreviewControl previewControl = viewsEditorControl.PreviewControl;

//            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();
//            ClassDef classDef = (ClassDef)converter.Convert(dmClass);
//            IUIDef uiDef = classDef.UIDefCol[viewName];
//            xmlEditorControl.Stub(control => control.CurrentUIDef).Return(uiDef);

//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.PreviewFormButton.PerformClick();
//            //---------------Test Result -----------------------
//            previewControl.AssertWasCalled(control => control.PreviewForm(classDef, uiDef.UIForm));
//        }

//        [Test]
//        public void Test_PreviewFormButton_WhenNotHasClassDef_ShouldNotRunPreviewGrid()
//        {
//            //---------------Set up test pack-------------------
//            DMClass dmClass = TestUtilsLogic.CreateClassWithOneUIView();
//            string viewName = dmClass.UIViews[0].ViewName;
            
//            UIViewsEditorControl viewsEditorControl = CreateUIViewsEditorControl();
//            IUIXmlEditorControl xmlEditorControl = viewsEditorControl.XMLEditorControl;
//            IUIPreviewControl previewControl = viewsEditorControl.PreviewControl;


//            DMToEntityClassDefsConverter converter = new DMToEntityClassDefsConverter();
//            ClassDef classDef = (ClassDef)converter.Convert(dmClass);
//            IUIDef uiDef = classDef.UIDefCol[viewName];
//            xmlEditorControl.Stub(control => control.CurrentUIDef).Return(uiDef);

//            viewsEditorControl.EditUIViews(dmClass.UIViews);
//            //---------------Execute Test ----------------------
//            viewsEditorControl.PreviewFormButton.PerformClick();
//            //---------------Test Result -----------------------
//            previewControl.AssertWasNotCalled(control => control.PreviewForm(Arg<IClassDef>.Is.Anything, Arg<IUIForm>.Is.Anything));
//        }
//    }
//}
